<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改项目')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-project-edit" th:object="${zknjProject}">
        <input name="proId" th:field="*{proId}" type="hidden">
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">名称：</label>
            <div class="col-sm-8">
                <input name="proName" th:field="*{proName}" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">描述：</label>
            <div class="col-sm-8">
                <input name="proDes" th:field="*{proDes}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">状态：</label>
            <div class="col-sm-8">
                <label class="toggle-switch switch-solid">
                    <input type="checkbox" name="status" id="status" th:checked="*{isLock == 0}">
                    <span></span>
                </label>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">项目结束日期：</label>
            <div class="col-sm-8">
                <div class="input-group date">
                    <input name="endTime" th:value="${#dates.format(zknjProject.endTime, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text">
                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<script th:inline="javascript">
    var prefix = ctx + "system/project";
    var proStatus = [[${zknjProject.proStatus}]];
    var dataOld;

    $("#form-project-edit").validate({
        onkeyup: false,
        rules: {
            proName: {
                remote: {
                    url: prefix + "/checkProNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "proName": function () {
                            return $.common.trim($("#proName").val());
                        },
                        "proId": function () {
                            return $("#proId").val();
                        }
                    }
                }
            }
        },
        messages: {
            "proName": {
                remote: "该项目已经存在",
            }
        },
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var data = [];
            var dataNew = $('#form-project-edit').serializeArray();
            if(dataOld.length == dataNew.length){
                for (let i = 0; i < dataNew.length; i++) {
                    if(!shallowEqual(dataNew[i], dataOld[i])) {
                        data.push(dataNew[i]);
                    }
                }
            }
            if(data.length > 0 || dataOld.length != dataNew.length){
                // alert(JSON.stringify(dataNew));
                // data.push({"name": "proStatus", "value": 2});
                var status = $("input[id='status']").is(':checked') == true ? 0 : 1;
                dataNew.push({"name": "proStatus", "value": status});
                $.operate.save(prefix + "/edit", dataNew);
            }else {
                $.modal.close();
            }
        }
    }

    $("input[name='endTime']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    $(function() {
        dataOld = $('#form-project-edit').serializeArray();

    })
</script>
</body>
</html>